// pages/publish/manageTask.js
const httpUtils = require('../../utils/httpUtils')
const ui = require('../../utils/ui')
var currentIndex = 0;

Page({
  data: {
    publishdaishenhe: 0,
    publishdaifukuan: 0,
    publishjinxingzhong: 0,
    publishdengdaijieshu: 0,
    publishyijieshu: 0,
    publishshenheshibai: 0,
    cellsOne: [],
    cellsTwo: [],
    cellsThree: [],
    cellsFour: [],
    cellsFive: [],
    cellsSix: [],
    page: 1,
    activeTab: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    currentIndex = options.index;
    this.setData({
      activeTab: parseInt(options.index)
    });
    this.requestList();
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.setData({
      page: 1
    });
    this.requestList();
    wx.stopPullDownRefresh();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.setData({
      page: this.data.page + 1
    });
    this.requestList();
  },
  //标签切换
  onChange(event) {
    currentIndex = event.detail.index;
    this.setData({
      page: 1,
      activeTab: parseInt(event.detail.index)
    });
    this.requestList();
  },
  //查看任务详情
taskDetailRequest(event) {
  wx.navigateTo({
    url: '/pages/task/taskDetail?taskNo='+event.currentTarget.dataset.taskno,
  })
},
//任务进程
actionProgress(event) {
  wx.navigateTo({
    url: '/pages/publish/manageProgress?taskNo='+event.currentTarget.dataset.taskno,
  })
},
//增加任务份额
actionAddTask(event) {
  wx.navigateTo({
    url: '/pages/publish/publishAddTask?taskNo='+event.currentTarget.dataset.taskno+'&price='+event.currentTarget.dataset.price,
  })
},
//重新发布
actionPublishTask(event) {
  wx.navigateTo({
    url: '/pages/publish/publishTaskEdit?againCreate=1&taskNo='+event.currentTarget.dataset.taskno,
  })
},
//查看提交结果
actionExamineTask(event) {
wx.navigateTo({
  url: '/pages/publish/examineTask?taskNo='+event.currentTarget.dataset.taskno,
})
},
//修改任务
actionEditTask(event) {
  wx.navigateTo({
    url: '/pages/publish/publishTaskEdit?taskNo='+event.currentTarget.dataset.taskno,
  })
},
//结束任务
actionEndTask(event) {
  wx.showModal({
    title: '提示',
    content: '是否要结束当前任务？结束后用户无法领取任务',
    complete: (res) => {
      if (res.confirm) {
        let obj = {
          method: 'PUT',
          contentType: 'application/x-www-form-urlencoded',
          url: '/task-management/' + event.currentTarget.dataset.taskno + '/stop',
          data: {}
        }
        httpUtils.request(obj).then(res => {
          this.onPullDownRefresh();
        }).catch(err => {
          console.log('ERROR')
        });
      }
    }
  });
},
//删除任务
actionDeleteTask(event) {
  wx.showModal({
    title: '提示',
    content: '是否要删除当前任务？',
    complete: (res) => {
      if (res.confirm) {
        let obj = {
          method: 'DELETE',
          contentType: 'application/x-www-form-urlencoded',
          url: '/task/published/' + event.currentTarget.dataset.taskno,
          data: {}
        }
        httpUtils.request(obj).then(res => {
          this.onPullDownRefresh();
        }).catch(err => {
          console.log('ERROR')
        });
      }
    }
  });
},
//支付任务
actionPayTask(event) {
  wx.navigateTo({
    url: '/pages/publish/pay/publishPayTask?taskNo='+event.currentTarget.dataset.taskno+'&price='+event.currentTarget.dataset.price,
  })
},
//请求列表
requestList() {
  var typeStr = "待审核";
  if (currentIndex == 1) {
    typeStr = "待付款";
  } else if (currentIndex == 2) {
    typeStr = "进行中";
  } else if (currentIndex == 3) {
    typeStr = "等待结束";
  } else if (currentIndex == 4) {
    typeStr = "已结束";
  } else if (currentIndex == 5) {
    typeStr = "审核失败";
  }
  let obj = {
    method: 'GET',
    url: '/task/user/published',
    showLoading: true,
    data: {
      page: this.data.page,
      size: 20,
      taskStatus: typeStr
    }
  }
  httpUtils.request(obj).then(res => {
    if (currentIndex == 5) {
      if (this.data.page == 1) {
        this.setData({
          cellsSix: []
        });
      }
      const {
        cellsSix = []
      } = this.data;
      res.data.taskPublishListDetails.forEach(function (item, index) {
        cellsSix.push(item);
      });
      this.setData({
        cellsSix,
        publishshenheshibai: res.data.count
      });
    } else if (currentIndex == 4) {
      if (this.data.page == 1) {
        this.setData({
          cellsFive: []
        });
      }
      const {
        cellsFive = []
      } = this.data;
      res.data.taskPublishListDetails.forEach(function (item, index) {
        cellsFive.push(item);
      });
      this.setData({
        cellsFive,
        publishyijieshu: res.data.count
      });
    } else if (currentIndex == 3) {
      if (this.data.page == 1) {
        this.setData({
          cellsFour: []
        });
      }
      const {
        cellsFour = []
      } = this.data;
      res.data.taskPublishListDetails.forEach(function (item, index) {
        cellsFour.push(item);
      });
      this.setData({
        cellsFour,
        publishdengdaijieshu: res.data.count
      });
    } else if (currentIndex == 2) {
      if (this.data.page == 1) {
        this.setData({
          cellsThree: []
        });
      }
      const {
        cellsThree = []
      } = this.data;
      res.data.taskPublishListDetails.forEach(function (item, index) {
        cellsThree.push(item);
      });
      this.setData({
        cellsThree,
        publishjinxingzhong: res.data.count
      });
    } else if (currentIndex == 1) {
      if (this.data.page == 1) {
        this.setData({
          cellsTwo: []
        });
      }
      const {
        cellsTwo = []
      } = this.data;
      res.data.taskPublishListDetails.forEach(function (item, index) {
        cellsTwo.push(item);
      });
      this.setData({
        cellsTwo,
        publishdaifukuan: res.data.count
      });
    } else {
      if (this.data.page == 1) {
        this.setData({
          cellsOne: []
        });
      }
      const {
        cellsOne = []
      } = this.data;
      res.data.taskPublishListDetails.forEach(function (item, index) {
        cellsOne.push(item);
      });
      this.setData({
        cellsOne,
        publishdaishenhe: res.data.count
      });
    }
    
  }).catch(err => {
    console.log(err)
  });
}
})